package ru.dc.object;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 02.11.2010
 * Time: 16:00:29
 * </pre>
 */
public class Settings {

    public static final int MODE_PASSIVE = 0;
    public static final int MODE_ACTIVE = 1;

    private String nick;
    private String email;
    private String encoding;
    private int mode = MODE_PASSIVE;
    private String downloadDir;
    private String externalAddress;
    private boolean getAddressFromHub = true;
    private int tcpPort = 8181;
    private int udpPort = 8184;

    private Log logger = LogFactory.getLog(getClass());


    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public String getDownloadDir() {
        return downloadDir;
    }

    public void setDownloadDir(String downloadDir) {
        this.downloadDir = downloadDir;
    }

    public String getExternalAddress() {
        return externalAddress;
    }

    public void setExternalAddress(String externalAddress) {
        this.externalAddress = externalAddress;
    }

    public boolean isGetAddressFromHub() {
        return getAddressFromHub;
    }

    public void setGetAddressFromHub(boolean getAddressFromHub) {
        this.getAddressFromHub = getAddressFromHub;
    }

    public Log getLogger() {
        return logger;
    }

    public void setLogger(Log logger) {
        this.logger = logger;
    }

    public int getTcpPort() {
        return tcpPort;
    }

    public void setTcpPort(int tcpPort) {
        this.tcpPort = tcpPort;
    }

    public int getUdpPort() {
        return udpPort;
    }

    public void setUdpPort(int udpPort) {
        this.udpPort = udpPort;
    }

    public void save() {
        String home = System.getProperty("user.home");
        File appDir = new File(home, ".yadcc");
        appDir.mkdirs();
        File settings = new File(appDir, "settings");
        Properties p = new Properties();
        p.put("nick", nick!=null ? nick : "");
        p.put("email", email!=null ? email : "");
        p.put("mode", mode+"");
        p.put("encoding", encoding!=null ? encoding : "");
        p.put("downloadDir", downloadDir!=null ? downloadDir : "");
        p.put("externalAddress", externalAddress!=null ? externalAddress: "");
        p.put("getAddressFromHub", Boolean.toString(getAddressFromHub));
        p.put("tcpPort", Integer.toString(tcpPort));
        p.put("udpPort", Integer.toString(udpPort));

        try {
            p.store(new FileOutputStream(settings), "settings");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void load() {
        String home = System.getProperty("user.home");
        File appDir = new File(home, ".yadcc");
        File settings = new File(appDir, "settings");
        Properties p = new Properties();
        try {
            p.load(new FileInputStream(settings));

            setNick(p.getProperty("nick"));
            setEmail(p.getProperty("email"));
            try {
                setMode(new Integer(p.getProperty("mode")));
            } catch (NumberFormatException e) {
                logger.warn("Can't parse mode: ",e);
            }
            setEncoding(p.getProperty("encoding"));
            setDownloadDir(p.getProperty("downloadDir"));
            setExternalAddress(p.getProperty("externalAddress"));
            setGetAddressFromHub(Boolean.valueOf(p.getProperty("getAddressFromHub")));
            try {
                setTcpPort(new Integer(p.getProperty("tcpPort")));
            } catch (NumberFormatException e) {
                logger.warn("Can't parse tcpPort: ",e);
            }
            try {
                setUdpPort(new Integer(p.getProperty("udpPort")));
            } catch (NumberFormatException e) {
                logger.warn("Can't parse udpPort: ",e);
            }

        } catch (IOException e) {
            logger.error("Can't load settings", e);
        }


    }

}
